package com.project.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.project.bean.SystemInfo.RecordBean;
import com.project.bean.SystemInfo.UserBean;
import com.project.mapper.IUserMapper;
import com.project.service.IRecordService;
import com.project.service.IUserService;
import com.project.until.JWTUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDate;
import java.util.List;


@Service
@Transactional
public class IUserServiceImpl extends ServiceImpl<IUserMapper, UserBean> implements IUserService {


    @Override
    public IPage<UserBean> findCutUser(int pageNo, int pageSize, String userRoot) {

        Page<UserBean> page = new Page<>(pageNo,pageSize);

        QueryWrapper wrapper = new QueryWrapper<>();

       if (userRoot != null){

           wrapper.like("user_root",userRoot);

       }

        this.baseMapper.selectPage(page,wrapper);

        return this.baseMapper.selectPage(page,wrapper);
    }

    @Override
    public UserBean Login(String userName, String userPwd) {

        QueryWrapper wrapper = new QueryWrapper<>();
        wrapper.eq("user_name",userName);
        wrapper.eq("user_pwd",userPwd);
        UserBean userBean = this.baseMapper.selectOne(wrapper);

        return userBean;
    }

    @Override
    public void addUser(UserBean userBean) {

        List<UserBean> list =  findAllUser();

        for (UserBean u :list) {

            if (userBean.getUserName().equals(u.getUserName())){
                return;
            }

        }
        this.baseMapper.insert(userBean);


    }

    @Override
    public void delUser(int userId) {

        this.baseMapper.deleteById(userId);

    }

    @Override
    public void updateUser(int userId, String userPwd,String userRoot) {

        UserBean userBean = new UserBean();
        userBean.setUserId(userId);
        userBean.setUserPwd(userPwd);
        userBean.setUserRoot(userRoot);

        this.baseMapper.updateById(userBean);
    }

    @Override
    public UserBean findUser(int userId) {

        return this.baseMapper.selectById(userId);

    }

    @Override
    public List<UserBean> findAllUser() {
        List<UserBean> list = this.baseMapper.selectList(null);
        return list;
    }


}
